SkillEffect = class("SkillEffect")

function SkillEffect:ctor( ... )
	local arg = {...}
	if(#arg ~= 3) then
		GameLog.Error("SkillEffect ctor param number is not 3.")
		return
	end

	self.id = arg[1]
	self.effectData = arg[2]
	self.role = arg[3]
end

function SkillEffect:play() 
	self.updateTime = 0
    --GameLog.Error(self.effectData.res_name)
	self.res = ObjectPoolManager:GetObjectForce(self.effectData.res_name)
	self.transform = self.res:GetComponent(typeof(UnityEngine.Transform))
	self:bind()
	--[[
	if(not self.role.isFriend) then
		self.transform.eulerAngles = Vector3.New(0,180,0)
	end
	local position = self.role.transform.position
	local x 
	if(self.role.isFriend) then 
		x = position.x + self.effectData.offset.x
	else 
		x = position.x - self.effectData.offset.x
	end
	
	local y = position.y + self.effectData.offset.y
	self:updatePos(position, x, y)
	--self.transform.position = position
	]]
	
	self.animationController = self.res:GetComponent(typeof(SpineAnimationController))
	if(self.animationController == nil) then
		self.animationController = self.res:AddComponent(typeof(SpineAnimationController))
		if(self.animationController == nil) then
			GameLog.Warning("self.animationController is nil")
		end
	end
	self.animationController:PlayAnimation(self.effectData.anim_name, self.effectData.anim_is_loop)
	self.isPlaying = true

	UpdateBeat:Add(self.update, self)
end

function SkillEffect:bind()
	local nodeTransform = self.role.transform:Find(self.effectData.bind_node)
	self.transform:SetParent(nodeTransform)
	self.transform.localPosition = Vector3.New(self.effectData.bind_offset.x, self.effectData.bind_offset.y, 0)
	local position = self.transform.position
	--GameLog.Warning("local position x = " .. position.x .. " y = " .. position.y .. " z = " .. position.z)
	self:updatePos(self.role.transform.position, position.x, position.y)
end

--[[
function SkillEffect:updatePos(pos)
	local position = pos
	position.z = BattleSort.calZValue(position.x, position.y)
	position.z = BattleSort.calZValueWithShift(position.z, "effect")
	self.transform.position = position
end
]]


function SkillEffect:updatePos(pos, x, y)
	local position = pos
	position.z = BattleSort.calZValue(position.x, position.y)
	position.z = BattleSort.calZValueWithShift(position.z, "effect")
	position.x = x
	position.y = y
	self.transform.position = position
end

function SkillEffect:update()
	self.updateTime = self.updateTime + Time.deltaTime
	if(self.isPlaying and self.updateTime >= self.effectData.duration) then
		self.isPlaying = false
		self.animationController:StopAnimation()
		ActionPlayer.removeEffect(self.id)
	end
end

function SkillEffect:destroy()
	ObjectPoolManager:ReturnObject(self.effectData.res_name, self.res)

	self.effectData = nil
	self.role = nil

	self.res = nil
	self.transform = nil
	self.animationController = nil

	UpdateBeat:Remove(self.update, self)
end